package com.soft.knowledge.controller;


import com.alibaba.fastjson.JSONObject;
import com.soft.knowledge.req.UserLoginReq;
import com.soft.knowledge.req.UserQueryReq;
import com.soft.knowledge.req.UserResetPasswordReq;
import com.soft.knowledge.req.UserSaveReq;
import com.soft.knowledge.resp.CommonResp;
import com.soft.knowledge.resp.PageResp;
import com.soft.knowledge.resp.UserLoginResp;
import com.soft.knowledge.resp.UserQueryResp;
import com.soft.knowledge.service.UserService;
import com.soft.knowledge.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 电子书(User)表控制层
 *
 * @author makejava
 * @since 2022-11-19 13:17:14
 */
@RestController
@RequestMapping("user")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 通过请求参数列表name属性进行模糊查询
     *
     * @param req 请求参数列表
     * @return 多条数据
     */
    @GetMapping("/list")
    public CommonResp list(@Valid UserQueryReq req){
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        PageResp<UserQueryResp> list = userService.list(req);
        resp.setContent(list);
        resp.setMessage("查询成功！");
        return resp;
    }
    @PostMapping("/save")
    public CommonResp save(@RequestBody @Valid UserSaveReq req){//post json方式请求要加@RequestBody，form表单方式提交不需要加该注解
        //用户密码加密存储
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        resp.setMessage("保存成功");
        return resp;
    }
    //restful
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp = new CommonResp<>();
        userService.delete(id);
        resp.setMessage("删除成功");
        return resp;
    }

    @PostMapping("/reset-password")
    public CommonResp resetPassword(@RequestBody @Valid UserResetPasswordReq req){//post json方式请求要加@RequestBody，form表单方式提交不需要加该注解
        //用户密码加密存储
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        userService.resetPassword(req);
        resp.setMessage("修改成功");
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@RequestBody @Valid UserLoginReq req){//post json方式请求要加@RequestBody，form表单方式提交不需要加该注解
        //用户密码加密存储
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp userLoginResp = userService.login(req);

        Long token = snowFlake.nextId();
        LOG.info("生成单点登录token：{}，并放入redis中", token);
        userLoginResp.setToken(token.toString());
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResp), 3600 * 24, TimeUnit.SECONDS);

        resp.setContent(userLoginResp);
        resp.setMessage("登陆成功");
        return resp;
    }

    @GetMapping(value = "/logout/{token}")
    public CommonResp logout(@PathVariable Long token){
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        LOG.info("从redis中删除token：{}", token);
        resp.setMessage("退出登录成功");
        return resp;
    }
}